package com.dashu.authority.shiro;

import java.util.HashSet;
import java.util.Set;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.dashu.authority.common.ActiveAccount;
import com.dashu.authority.entity.User;
import com.dashu.authority.exception.AuthorityException;
import com.dashu.authority.servie.AuthenticationInfoService;
import com.dashu.authority.servie.AuthorityOperateService;
import com.dashu.authority.servie.UserService;
import com.dashu.authority.util.Md5Util;

/**
 * 用户账号信息数据源
 */
@Component
public class AuthAccountRealm extends AuthorizingRealm {

    @Autowired
    private UserService userService;
    
    @Autowired
    private AuthenticationInfoService infoService;
    
    @Autowired
    private AuthorityOperateService authService;
    
    

    @Override
    public void setName(String name) {
        super.setName("authAccountRealm");
    }

    /**
     * 用户认证
     *
     * @param token
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String username = (String) token.getPrincipal();
        //根据账号获取用户信息
        User account=null;
		try {
			account = userService.findAccountWithUserName(username);
		} catch (AuthorityException e) {
			account=null;
		}
        //如果用户为空返回
        if (account == null) return null;
        //根据查询到的信息设置当前登录的的用户信息
        ActiveAccount activeAccount = new ActiveAccount();
        activeAccount.setUser(account);
    	String key;
		try {
			key = infoService.insertEntity(username,"");
		} catch (AuthorityException e) {
			 return null;
		}
        activeAccount.setToken(key);
        
        //设置凭证信息返回
        return new SimpleAuthenticationInfo(activeAccount,
                ByteSource.Util.bytes("1"),
                getName());
    }

    @Override
    protected AuthorizationInfo getAuthorizationInfo(PrincipalCollection principals) {
    	// TODO Auto-generated method stub
    	return super.getAuthorizationInfo(principals);
    }
    /**
     * 对认证通过的账号进行授权
     *
     * @param principals
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        //认证凭证中获取当前登录账号的信息
    	User activeAccount = (User) principals.getPrimaryPrincipal();
        //获取账号权限
//      Set<String> permissions = authService.
    	Set<String> permissions =new HashSet<String>();
    	permissions.add("123");
    	permissions.add("123");
        //设置认证凭证
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        authorizationInfo.setStringPermissions(permissions);
        return authorizationInfo;
    }
    
    /**
     * 清除缓存
     */
    public void clearAuthAccountCache() {
        super.clearCache(SecurityUtils.getSubject().getPrincipals());
    }

    @Override
    public void clearCachedAuthorizationInfo(PrincipalCollection principals) {
        super.clearCachedAuthorizationInfo(principals);
    }

    @Override
    public void clearCachedAuthenticationInfo(PrincipalCollection principals) {
        super.clearCachedAuthenticationInfo(principals);
    }

    @Override
    public void clearCache(PrincipalCollection principals) {
        super.clearCache(principals);
    }

    public void clearAllCachedAuthorizationInfo() {
        getAuthorizationCache().clear();
    }

    public void clearAllCachedAuthenticationInfo() {
        getAuthenticationCache().clear();
    }

    public void clearAllCache() {
        clearAllCachedAuthenticationInfo();
        clearAllCachedAuthorizationInfo();
    }
}
